require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#coupon",
    data:{
        loginUid:userInfo.loginUid,
        productId:"",
        coupons:{},
        recsList:[]
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.productId=this.searchObject&&this.searchObject.productId;
        this.getCouponsByProductId();
        // this.cartCount()
    },
    methods:{
        //后退
        getBack: function () {
            history.go(-1);
            // if(this.square==1){
            //     location.href="/friend/circleSquare";
            // }else{
            //     location.href="/friend?indexs=3";
            // }
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //优惠券列表
        getCouponsByProductId:function(){
            var that=this,data;
            data={
                loginUid:that.loginUid,
                productId:that.productId
            };
            this.ajax({url:"/purchase/getCouponsByProductId",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.coupons=b.json.data.coupons[0];
                    that.recsList=b.json.data.recs;
                    // that.luckyVoucher=that.mainList.luckyVoucher[0];
                    // that.shopsList=that.mainList.hotsShops[0];
                    // that.categoryId=that.shopsList.categoryId;
                    // that.endDate=that.luckyVoucher.endDate;
                    // that.endSecond=parseInt(that.endDate)*60*60;
                    // that.compareDate(that.luckyVoucher.currentTime);
                    // console.log(that.endDate)
                    // console.log(that.endSecond)
                }else{
                    vm.$refs.prompt.toast("获取优惠券列表失败！")
                }
            });
        },
        compareDate:function(item){
            var that=this;
            console.log(item)
            var dataList=item.split(":");
            var hour,minute,second;
            hour=parseInt(that.endDate)-parseInt(dataList[0]);
            if(dataList[1]>0||dataList[2]>0){
                hour--;
                minute=60-parseInt(dataList[1]);
                if(dataList[2]>0){
                    minute--;
                    second=60-parseInt(dataList[2]);
                }
            }
            console.log(hour+"时"+minute+"分"+second+"秒");
            that.hours=hour;
            that.minutes=minute;
            that.seconds=second;
            a=setInterval(this.leftTimer,1000);
            //this.leftTimer();
        },
        //时分秒倒计时
        leftTimer:function(){
            var that=this;
            that.hours=Number(that.hours);
            that.minutes=Number(that.minutes);
            that.seconds=Number(that.seconds);
            if(that.hours==0&&that.minutes==0&&that.seconds==0){
                clearInterval(a);
            }else{
                that.seconds--;
                if(that.seconds == 0){
                    that.minutes--;
                    that.seconds = 59;
                }
                if(that.minutes < 0 && that.hours > 0){
                    that.hours--;
                    that.minutes = 59;
                }
                if (that.hours <= 9) that.hours = '0' + that.hours;
                if (that.minutes <= 9) that.minutes = '0' + that.minutes;
                if (that.seconds <= 9) that.seconds = '0' + that.seconds;
            }
        },
        //跳转页面
        getNext:function(couponId){
            if(couponId){
                location.href="/purchase/couponInfo?couponId="+couponId;
            }else{
                vm.$refs.prompt.toast("没有更多！")
            }

        }
    }
});
var a;
function checkTime(i){ //将0-9的数字前面加上0，例1变为01
    if(i<10){
        i = "0" + i;
    }
    return i;
}
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
    return scrollTop;
}
window.onscroll=function(){
    // if(getDocumentTop()>120){
    //     vm.isNav=2;
    // }else{
    //     vm.isNav=1;
    // }
};
